package com.ecar.api.rest.manger;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ecar.api.rest.AbstractResource;
import com.ecar.api.service.manager.LogLoginService;
import com.ecar.api.service.manager.LogOpsService;
import com.ecar.common.dto.DataBean;
import com.ecar.common.dto.DataBeanEcar;
import com.ecar.common.dto.Pager;
import com.ecar.common.util.JSONUtil;


@Component
@Scope("prototype")
@Path("/log")
public class LogController extends AbstractResource {
	@Autowired
	LogOpsService logOpsService;
	
	@Autowired
	LogLoginService logLoginService;
	
	@Context
	HttpServletRequest request;

	@Context
	HttpServletResponse response;

	@Context
	UriInfo uriInfo;

	/**
	 * 获取登录日志分页记录
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@GET
	@Path("/login/{pageNumber}/{pageSize}/page")
	@Produces(MediaType.APPLICATION_JSON)
	public DataBeanEcar pageLogin(
			@PathParam("pageNumber") int pageNumber,
			@PathParam("pageSize") int pageSize) {
		try {
			Pager pager = new Pager();
			HashMap<Object,Object> queryMap = new HashMap();
			if (pageNumber <= 0 || pageSize <= 0)
				return new DataBeanEcar(400, "参数错误。");

			parseParams(request);
			if (params != null)
				queryMap = JSONUtil.deserialize(params, HashMap.class);
			
			if (!params.has("loginTime")) {
				queryMap.put("loginTime1", null);
				queryMap.put("loginTime2", null);
			}else{
				String[] a = params.getString("loginTime").split("#@");
				queryMap.put("loginTime1", a[0]);
				queryMap.put("loginTime2", a[1]);
			}

			pager.setQueryMap(queryMap);
			pager.setPageNumber((pageNumber-1)*10);
			pager.setPageSize(pageSize);
			return logLoginService.getByPager(pager);
		} catch (Exception e) {
			e.printStackTrace();
			return new DataBeanEcar(500, "查找登录日志列表失败!");
		}
	}

	/**
	 * 获取操作日志分页
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	
	@GET
	@Path("/ops/{pageNumber}/{pageSize}/page")
	@Produces(MediaType.APPLICATION_JSON)
	public DataBean pageOps(
			@PathParam("pageNumber") int pageNumber,
			@PathParam("pageSize") int pageSize) {
		try {
			Pager pager = new Pager();
			HashMap<Object,Object> queryMap = null;
			if (pageNumber <= 0 || pageSize <= 0)
				return new DataBean(400, "参数错误。");

			parseParams(request);
			if (this.params != null)
				queryMap = JSONUtil.deserialize(this.params, HashMap.class);
			
			if (!params.has("loginTime")) {
				queryMap.put("loginTime1", null);
				queryMap.put("loginTime2", null);
			}else{
				String[] a = params.getString("loginTime").split("#@");
				queryMap.put("loginTime1", a[0]);
				queryMap.put("loginTime2", a[1]);
			}

			pager.setQueryMap(queryMap);
			pager.setPageNumber(pageNumber);
			pager.setPageSize(pageSize);
			return logOpsService.getByPager(pager);
		} catch (Exception e) {
			e.printStackTrace();
			return new DataBean(500, "查找操作日志列表失败。");
		}
	}

}
